package web.importations.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 21/09/12
 * Time: 11:59
 * To change this template use File | Settings | File Templates.
 */

@ResultPath("/")
public class ProtocolSelectController extends ActionSupport {
    private List<Protocolo> protocolList = new ArrayList<Protocolo>();

    private DrogaService drogaService;
    private CompromisoPaisService paisService;

    private String paisId;
    private String idsDrogas;


    @Autowired
    public ProtocolSelectController(DrogaService drogaService, CompromisoPaisService paisService, ProtocolCRUDService protService){
        this.drogaService = drogaService;
        this.paisService = paisService;
        protocolList = new ArrayList<Protocolo>();
    }

    @Action(value="fillProtocolList", results={
            @Result(name="success", location="WEB-INF/jsp/Importations/protocols.jsp")
    })
    public String fillProtocolList() {
        protocolList.clear();

        String [] drogasStr = idsDrogas.split(", ");
        List<Droga> drogas = new ArrayList<Droga>();
        for(int i = 0; i < drogasStr.length; i++){
            if(!drogasStr[i].equals("") && !drogasStr[i].equals(" ") )
                drogas.add(drogaService.findById(Integer.parseInt(drogasStr[i])));
        }

        if( !drogas.isEmpty() && !paisId.equals("") ){

            Compromisopais paisBuscar = paisService.findById(Integer.parseInt(paisId));

            Iterator<Droga> itDrogas = drogas.iterator();
            protocolList.addAll(itDrogas.next().getProtocolos());
            while(itDrogas.hasNext() && !protocolList.isEmpty()){
                protocolList.retainAll(itDrogas.next().getProtocolos());
            }

            if(protocolList.isEmpty())
                return SUCCESS;

            Iterator<ProtocoloCompromisopais> itPcp = paisBuscar.getProtocoloCompromisopaises().iterator();
            List<Protocolo> paisProtocols = new ArrayList<Protocolo>();
            while(itPcp.hasNext()){
                paisProtocols.add(itPcp.next().getProtocolo());
            }

            protocolList.retainAll(paisProtocols);
        }

        return SUCCESS;
    }

    public void setIdsDrogas(String idsDrogas) {
        this.idsDrogas = idsDrogas;
    }

    public void setPaisId(String paisId) {
        this.paisId = paisId;
    }

    public void setProtocolList(List<Protocolo> protocolList) {
        this.protocolList = protocolList;
    }

    public String getIdsDrogas() {
        return idsDrogas;
    }

    public String getPaisId() {
        return paisId;
    }

    public List<Protocolo> getProtocolList() {
        return protocolList;
    }

}
